/*
 * ****************************************************************
 * Copyright � 2010 Documentatm (TM) Limited. All rights reserved.
 * DOCUMENTA PROPRIETARY/CONFIDENTIAL. Use is subject to license terms.
 * This software is the confidential and proprietary information of
 * Documentatm (TM) Limited ("Confidential Information").
 * You shall not disclose such Confidential Information and shall use
 * it only in accordance with the terms of the license agreement
 * you entered into with Documentatm (TM) Limited.
 * *****************************************************************
 */

var SETUP = {
    save: function(data, url) {
        $.ajax({
            url: url + '/save',
            type: 'post',
            dataType: "json",
            data: data,
            success: function(json) {
                if (json.type == 1) {
                    $("#jqgrid-grid").trigger("reloadGrid");
                    reset_form();
                }
                MessageRenderer.render(json)
            }
        });
    },
    update: function(data, url) {
        $.ajax({
            url: url + '/update',
            type: 'post',
            dataType: "json",
            data: data,
            success: function(json) {
                if (json.type == 1) {
                    $("#jqgrid-grid").trigger("reloadGrid");
                    reset_form();
                }
                MessageRenderer.render(json);
            }
        });
    },
    deleteAjax:function(data, url) {
        $("#dialog").dialog("destroy");
        $("#dialog-confirm").dialog({
            resizable: false,
            height:170,
            modal: true,
            title: 'Delete alert',
            buttons: {
                'Delete item(s)': function() {
                    $(this).dialog('close');
                    $.ajax({
                        type: "POST",
                        dataType: "json",
                        url: url + "/delete",
                        data: data,
                        success: function(message) {
                            if (message.type == 1) {
                                $("#jqgrid-grid").trigger("reloadGrid");
                                reset_form();
                            }
                            MessageRenderer.render(message)
                        }
                    });
                },
                Cancel: function() {
                    $(this).dialog('close');
                }
            }
        }); //end of dialog
    }
};

var DOCUAJAX = {

    save: function(data, url) {

        $.ajax({
            url: url + '/save',
            type: 'post',
            dataType: "html",
            data: data,
            success: function(html) {
                if (html.indexOf('com.docu.common.Message') > -1) {
                    MessageRenderer.render(JSON.parse(html))
                } else {
                    $('.ui-layout-content').fadeOut(function() {
                        $('.ui-layout-content').html(html).fadeIn();
                    });
                }
            }
        });
    },
    registration: function(data, url) {

        $.ajax({
            url: url + '/save',
            type: 'post',
            dataType: "html",
            data: data,
            success: function(html) {
                if (html.indexOf('com.docu.common.Message') > -1) {
                    MessageRenderer.render(JSON.parse(html))
                } else {
                    $('#registration-html').fadeOut(function() {
                        $('#registration-html').html(html).fadeIn();
                    });
                }
            }
        });
    },

    edit: function(data, url) {

        $.ajax({
            url: url + '/edit',
            type: 'post',
            dataType: "html",
            data: data,
            success: function(html) {
                if (html.indexOf('com.docu.common.Message') > -1) {
                    MessageRenderer.render(JSON.parse(html))
                } else {
                    $('.ui-layout-content').fadeOut(function() {
                        $('.ui-layout-content').html(html).fadeIn();
                    });
                }
            }
        });
    },
    update: function(data, url) {
        $.ajax({
            url: url + '/update',
            type: 'post',
            dataType: "html",
            data: data,
            success: function(html) {
                if (html.indexOf('com.docu.common.Message') > -1) {

                    MessageRenderer.render(JSON.parse(html))
                } else {
                    $('.ui-layout-content').fadeOut(function() {
                        $('.ui-layout-content').html(html).fadeIn();
                    });
                }
            }
        });
    }

};

function trim_form() {
    $(":input").not(":button, :submit, :reset, :checkbox").each(function () {
        this.value = $.trim(this.value);
    });
}

function reset_form() {
    $(":input").not(":button, :submit, :reset").each(function () {
        if (this.type.toLocaleLowerCase() == 'hidden') {
            this.value = "";
        }
        else if(this.type.toLocaleLowerCase() == 'select-one'){
            if($(this).attr('class')!='ui-pg-selbox')
            {
            $(this).val('');
            }
        }
        else if($(this).attr('class') !='ui-pg-input')
        {
           this.value = this.defaultValue;
        }
//        else {
//
//
//        }

    });
    $('input[type=checkbox]').attr('checked',false);
//    $('.ui-widget-content').removeClass('ui-state-highlight');
    $('#create-button').attr('value', 'Add');
    $('#delete-button').hide();
    $('#cancel-button').attr('value', 'Clear');
    $('form :input:visible:first').focus()
}

function checkForEnter (event) {

    currentBoxNumber = textboxes.index(this);
    if (event.keyCode == 13 && textboxes[currentBoxNumber].type !='textarea') {

        if ((textboxes[currentBoxNumber + 1] != null)) {
            nextBox = textboxes[currentBoxNumber + 1]
            nextBox.focus();
            nextBox.select();
            if (textboxes[currentBoxNumber].type == "button")
            {
                textboxes[0].focus();
//                textboxes[0].select();
            }
           else
           {
            event.preventDefault();
           }

//            return false;
        } else {
            textboxes[0].focus();
        }
    }
}

function resetFormByFormName(formName) {
    $(formName).find(":input").not(":button, :submit, :reset, :checkbox").each(function () {
        if (this.type == 'hidden') {
            this.value = "";
        }
        else if(this.type.toLocaleLowerCase() == 'select-one'){
            if($(this).attr('class')!='ui-pg-selbox')
            {
            $(this).val('');
            }
        }
        else if($(this).attr('class') !='ui-pg-input')
        {
           this.value = this.defaultValue;
        }
//        else {
//            this.value = this.defaultValue;
//        }
    });
    $(formName + ' input[type = checkbox]').attr('checked', false);
    $(formName + ' input[name = create-button]').attr('value', 'Add');
    $(formName + ' input[name = delete-button]').hide();
    $('#cancel-button').attr('value', 'Clear');
    $(formName+' :input:visible:first').focus()

//    $('.ui-widget-content').removeClass('ui-state-highlight');
}

var HtmlUtil = {

    createDropDown: function(data, selectedId, selectedName){
          $("#"+selectedId).children().remove().end().append('<option value="">--Select '+selectedName+'--</option>');
          $.each(data, function(key, value) {
             $("#"+selectedId).children().end().append('<option value="'+data[key].id+'">'+data[key].value+'</option>');

          });
    }
}


function setNumericField(inputId) {
    $("#" + inputId).format({precision:2,allow_negative:false,autofix:true});
}

function setIntegerField(inputId) {
    $("#" + inputId).format({precision:0,allow_negative:false,autofix:true});
}
function handleJqueryGridScroll(){
    if($('.ui-jqgrid-bdiv').get(0).scrollHeight > $('.ui-jqgrid-bdiv').get(0).clientHeight){
        $(".ui-jqgrid-hbox").css('padding-right','20px');
    } else {
        $(".ui-jqgrid-hbox").css('padding-right','0px');
    }
}


